﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace INSAForManagers
{
    public partial class FormDemerit : Form
    {
        DataGridView dgView;
        DataGridViewRow dgvRow;

        public FormDemerit()
        {
            InitializeComponent();
        }

        public FormDemerit(DataGridView dgv, DataGridViewRow row)
        {
            InitializeComponent();
            dgView = dgv;
            dgvRow = row;
        }

        private void FormDemerit_Load(object sender, EventArgs e)
        {
            try
            {
                if (dgView != null && dgvRow != null)
                {
                    txtPCode.Text = dgvRow.Cells[2].Value.ToString();
                    txtName.Text = dgvRow.Cells[3].Value.ToString();
                    txtTotalNow.Text = Math.Round(Double.Parse(dgvRow.Cells[9].Value.ToString()),2).ToString("#0.00");

                    if (DBUtil.existedDemeritData(txtPCode.Text) == true)
                    {
                        ArrayList arr = DBUtil.getDemeritInfo(txtPCode.Text);
                        if (arr != null)
                        {
                            txtComment.Text = arr[0].ToString();
                            txtTotalNow.Text = Math.Round(Double.Parse(arr[1].ToString()),2).ToString("#0.00");
                            numDemerit.Value = Decimal.Parse(arr[2].ToString());
                            txtTotalChange.Text = Math.Round(Double.Parse(arr[3].ToString()),2).ToString("#0.00");
                            lblRegtime.Text = arr[4].ToString();
                        }
                    }
                    else
                    {
                        lblRegtime.Text = "새로 입력...";

                        //삭제 버튼 비활성
                        btnDel.Enabled = false;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
            }
        }

        private void numDemerit_ValueChanged(object sender, EventArgs e)
        {
            double currentTotal = Double.Parse(txtTotalNow.Text);
            double changeValue = Double.Parse(numDemerit.Value.ToString());
            txtTotalChange.Text = (currentTotal - changeValue).ToString("#0.00");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtComment.Text.Length == 0)
            {
                MessageBox.Show("행정 처분 내용을 입력하세요!    ", "확인");
                txtComment.Focus();
                return;
            }

            if (numDemerit.Value == new Decimal(0))
            {
                MessageBox.Show("감점을 입력하세요!    ", "확인");
                numDemerit.Focus();
                return;
            }

            ArrayList arr = new ArrayList();
            DateTime now = DateTime.Now;
            arr.Add(txtPCode.Text);
            arr.Add(txtComment.Text);
            arr.Add(Double.Parse(txtTotalNow.Text));
            arr.Add(Double.Parse(numDemerit.Value.ToString()));
            arr.Add(Double.Parse(txtTotalChange.Text));
            arr.Add(now);

            if (DBUtil.writeDemeritInfo(arr) > 0)
            {

                //[수정됨] 조정점 수정
                dgvRow.Cells[8].Value = Double.Parse(numDemerit.Value.ToString());
                dgvRow.Cells[9].Value = txtTotalChange.Text;

                MessageBox.Show("반영 후 저장하였습니다.    ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);                
                this.Close();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            arr.Add(txtPCode.Text);
            if (DBUtil.deleteDemeritInfo(arr) > 0)
            {
                //총점을 원복 시킴
                dgvRow.Cells[8].Value = 0.0;
                dgvRow.Cells[9].Value = txtTotalNow.Text;

                //합산표 변동 사항 저장 여부 변경
                FormWorkMultiSum.isSavedChange = false;

                MessageBox.Show("삭제하였습니다.    ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
    }
}
